package com.supermap.desktop.ui.controls;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;
import com.supermap.desktop.Application;
import com.supermap.desktop.controls.ControlsProperties;
import com.supermap.desktop.properties.CommonProperties;

/**
 *打开数据库型工作空间
 * @author huchenpu
 */
public class JDialogWorkspaceOpenSQL extends javax.swing.JDialog {

    /**
     * 创建一个打开的配置窗口
     * @param DataBase 数据库类型(SQL/Oracle)
     */
    public JDialogWorkspaceOpenSQL(java.awt.Frame parent, boolean modal,String DataBase) {
        super(parent, modal);
        this.DataBase=DataBase;
        if("SQL".equals(DataBase)){
        	this.setTitle(ControlsProperties.getString("String_Title_OpenSQLWorkspace"));
        }else if("Oracle".equals(DataBase)){
			this.setTitle(ControlsProperties.getString("String_Title_OpenOracleWorkspace"));
		}
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    /**
     * 组件初始化
     */
    private void initComponents() {

        jButtonClose = new javax.swing.JButton();
        jButtonOpen = new javax.swing.JButton();
        jComboBoxServer = new javax.swing.JComboBox();
        jTextFieldDatabase = new javax.swing.JTextField();
        jTextFieldUser = new javax.swing.JTextField();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jComboBoxWorkspaceName = new javax.swing.JComboBox();
        jLabelServer = new javax.swing.JLabel();
        jLabelDatabase = new javax.swing.JLabel();
        jLabelUser = new javax.swing.JLabel();
        jLabelPassword = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabelEmptyName = new JLabel();
        jLabelEmptyServer = new JLabel();
        jLabelEmptyUser = new JLabel();
        jLabelEmptyLabel = new JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        // TODO
        jButtonClose.setText(CommonProperties.getString("String_Button_Cancel"));
        jButtonClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jButtonCloseActionPerformed(e);
			}
		});
        jButtonOpen.setText(CommonProperties.getString("String_Button_Open"));
        jButtonOpen.setPreferredSize(new java.awt.Dimension(75, 23));
        
        // TODO  Oracle或Sql Server实例列表初始化
        jComboBoxServer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { }));
        jComboBoxServer.setEditable(true);
        
        jComboBoxServer.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
				jComboBoxServerItemChange();
			}
			public void focusGained(FocusEvent e) {
				jComboBoxServerItemChange();
			}
		});
        
        jComboBoxServer.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent e) {
				jComboBoxServerItemChange();
			}
			
			public void keyReleased(KeyEvent e) {
				jComboBoxServerItemChange();
				
			}
			
			public void keyPressed(KeyEvent e) {
				jComboBoxServerItemChange();
			}
		});
        jComboBoxServer.addItemListener(new ItemListener() {
			
			public void itemStateChanged(ItemEvent e) {
				jComboBoxServerItemChange();
			}
		});

        jTextFieldDatabase.setText("");
        jTextFieldUser.setText("");
        jTextFieldUser.addFocusListener(new FocusListener() {
			
			public void focusLost(FocusEvent e) {
				jTextFieldUserValueChange();
			}
			
			public void focusGained(FocusEvent e) {
				jTextFieldUserValueChange();
			}
		});
        jTextFieldUser.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent e) {
				
			}
			
			public void keyReleased(KeyEvent e) {
				jTextFieldUserValueChange();
				
			}
			
			public void keyPressed(KeyEvent e) {
				
			}
		});
	

        jPasswordFieldPassword.setText("");
        

        jComboBoxWorkspaceName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ""}));
        jComboBoxWorkspaceName.setEditable(true);
        jComboBoxWorkspaceName.getComponent(0).addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent e){
        		jComboBoxWorkspaceNamemouseClicked(e);
        	}
		});
        
        jComboBoxWorkspaceName.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
				jComboBoxWorkspaceNameItemChange();
			}
			public void focusGained(FocusEvent e) {
				jComboBoxWorkspaceNameItemChange();
			}
		});

        jComboBoxWorkspaceName.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
			}
			public void keyReleased(KeyEvent e) {
				jComboBoxWorkspaceNameItemChange();
			}
			public void keyPressed(KeyEvent e) {
			}
		});
        
        jComboBoxWorkspaceName.addItemListener(new ItemListener() {
			
			public void itemStateChanged(ItemEvent e) {
				jComboBoxWorkspaceNameItemChange();
			}
		});
        if(DataBase.equals("Oracle")){
        	jLabelServer.setText(ControlsProperties.getString("String_Label_InstanceName"));
        }else if(DataBase.equals("SQL")){
        	jLabelServer.setText(ControlsProperties.getString("String_Label_ServersName"));
        }
        
        jLabelDatabase.setText(ControlsProperties.getString("String_Label_DatabaseName"));
        jLabelUser.setText(ControlsProperties.getString("String_Label_UserName"));
        jLabelPassword.setText(ControlsProperties.getString("String_Label_UserPassword"));
        jLabelName.setText(ControlsProperties.getString("String_Label_WorkspaceName"));

       

        jLabelEmptyServer.setText("!");
        jLabelEmptyServer.setVisible(false);
        jLabelEmptyUser.setText("!");
        jLabelEmptyUser.setVisible(false);
        jLabelEmptyName.setText("!");
        jLabelEmptyName.setVisible(false);
        if(DataBase.equals("Oracle")){
        	jLabelEmptyServer.setToolTipText(ControlsProperties.getString("String_ToolTipText_InstanceShouldNotEmpty"));
        }else if(DataBase.equals("SQL")){
        	jLabelEmptyServer.setToolTipText(ControlsProperties.getString("String_ToolTipText_ServersNameShouldNotEmpty"));
        }
        
        jLabelEmptyUser.setToolTipText(ControlsProperties.getString("String_ToolTipText_UserNameShouldNotEmpty"));
        jLabelEmptyName.setToolTipText(ControlsProperties.getString("String_ToolTipText_WorkSpaceNameShouldNotEmpty"));
        jButtonOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jButtonOpenActionPerformed(e);
			}
		});
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonOpen, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelServer)
                            .addComponent(jLabelDatabase)
                            .addComponent(jLabelUser)
                            .addComponent(jLabelPassword)
                            .addComponent(jLabelName))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(Alignment.LEADING)
                        	.addComponent(jLabelEmptyServer)
                        	.addComponent(jLabelEmptyLabel,10,10,10)
                        	.addComponent(jLabelEmptyUser)
                        	.addComponent(jLabelEmptyName))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxServer)
                            .addComponent(jTextFieldDatabase)
                            .addComponent(jTextFieldUser)
                            .addComponent(jPasswordFieldPassword)
                            .addComponent(jComboBoxWorkspaceName))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmptyServer)
                    .addComponent(jLabelServer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDatabase))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmptyUser)
                    .addComponent(jLabelUser))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmptyLabel)
                    .addComponent(jLabelPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxWorkspaceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmptyName)
                    .addComponent(jLabelName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonClose)
                    .addComponent(jButtonOpen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jButtonClose.getAccessibleContext().setAccessibleName("");
        jButtonOpen.getAccessibleContext().setAccessibleName("");

        pack();
        
        this.setSize(393, 228);
    }                       
    // </editor-fold> 

    /**
     * 工作空间下拉列表按钮点击事件
     * <li>标记出不能为空的项目
     * <li>查询数据库得出工作空间列表
     */
    private void jComboBoxWorkspaceNamemouseClicked(MouseEvent e){
    	/**
    	 * flag 标记是否有未填写项目
    	 */
    	boolean flag = false;
    	jComboBoxWorkspaceName.removeAllItems();
    	String jComboBoxServer=(String) this.jComboBoxServer.getSelectedItem();
    	if(null == jComboBoxServer || jComboBoxServer.length() <= 0){
    		//jLabelEmptyServer.setVisible(true);
    		flag = true;
    	}
    	String jTextFieldDatabase = this.jTextFieldDatabase.getText();
    	String jTextFieldUser = this.jTextFieldUser.getText();
    	if(null == jTextFieldUser || jTextFieldUser.length() <= 0){
    		//jLabelEmptyUser.setVisible(true);
    		flag = true;
    	}
    	String jPasswordFieldPassword = String.valueOf(this.jPasswordFieldPassword.getPassword());
    	if(true == flag){
    		//jLabelEmptyName.setVisible(true);
    		return ;
    	}
    	// TODO 查询工作空间列表
    	/*try {
    		Connection conn = null;
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:localhost:1521:" + jComboBoxServer;
		} catch (Exception e1) {
			e1.printStackTrace();
		}*/
    }
    
    /**
     * 工作空间下拉列表编辑框改变监听事件，根据结果是否为空提示信息。
     */
    private void jComboBoxWorkspaceNameItemChange(){
    	String jComboBoxWorkspaceName = ((JTextField) this.jComboBoxWorkspaceName.getEditor().getEditorComponent()).getText();
    	if(null == jComboBoxWorkspaceName || jComboBoxWorkspaceName.length() <= 0){
    		//jLabelEmptyName.setVisible(true);
    	}
    	else {
    		jLabelEmptyName.setVisible(false);
    	}
    }
   
    /**
     * open按钮点击事件
     * <li> 标记出不能为空的项目
     * <li> 打开工作空间
     */
    private void jButtonOpenActionPerformed(ActionEvent e){
    	int flag = 0;
    	String jComboBoxServer=(String) this.jComboBoxServer.getSelectedItem();
    	if(null == jComboBoxServer || jComboBoxServer.length() <= 0){
    		jLabelEmptyServer.setVisible(true);
    		flag = 1;
    	}
    	String jTextFieldDatabase = this.jTextFieldDatabase.getText();
    	String jTextFieldUser = this.jTextFieldUser.getText();
    	if(null == jTextFieldUser || jTextFieldUser.length() <= 0){
    		jLabelEmptyUser.setVisible(true);
    		flag = 1;
    	}
    	String jPasswordFieldPassword = String.valueOf(this.jPasswordFieldPassword.getPassword());
    	String jComboBoxWorkspaceName = (String) this.jComboBoxWorkspaceName.getSelectedItem();
    	if(null == jComboBoxWorkspaceName || jComboBoxWorkspaceName.length() <= 0){
    		jLabelEmptyName.setVisible(true);
    		flag = 1;
    	}
    	if(1 == flag){
    		return ;
    	}
    	
    	WorkspaceConnectionInfo Info = new WorkspaceConnectionInfo();
    	if ("Oracle".equals(DataBase)) {
    		Info.setType(WorkspaceType.ORACLE);
		} else if("SQL".equals(DataBase)){
			Info.setDriver("SQL SERVER");
			Info.setType(WorkspaceType.SQL);
		}
    	Info.setServer(jComboBoxServer);
    	Info.setDatabase(jTextFieldDatabase);
    	Info.setUser(jTextFieldUser);
    	Info.setPassword(jPasswordFieldPassword);
		Info.setName(jComboBoxWorkspaceName);
		try{
			boolean openResult = Application.getActiveApplication().getWorkspace().open(Info);
			String resultInfo = "";
			if (openResult) {
				resultInfo = ControlsProperties.getString("String_OpenWorkspaceSuccessful");
			} else {
				resultInfo = ControlsProperties.getString("String_OpenWorkspaceFailed");
			}
			Application.getActiveApplication().getOutput().output(resultInfo);
			if (openResult) {
				DialogExit();
			}
		}catch(Exception ex){
			ex.printStackTrace();
			Application.getActiveApplication().getOutput().output(ex);
		}
    }
    
    /**
     * 实例编辑框改变时判断是否显示提示信息
     */
    private void jComboBoxServerItemChange(){
    	String jComboBoxServer = ((JTextField) this.jComboBoxServer.getEditor().getEditorComponent()).getText();
    	if(null == jComboBoxServer || jComboBoxServer.length() <= 0){
    		//jLabelEmptyServer.setVisible(true);
    	}
    	else {
    		jLabelEmptyServer.setVisible(false);
    	}
    }
    
    /**
     *	 用户名输入框丢失焦点时判断是否为空
     */
    private void jTextFieldUserValueChange(){
    	String jTextFieldUser = this.jTextFieldUser.getText();
    	if(null == jTextFieldUser || jTextFieldUser.length() <= 0){
    		//jLabelEmptyUser.setVisible(true);
    	}
    	else {
    		jLabelEmptyUser.setVisible(false);
    	}
    }
    /**
     * 关闭按钮点击事件
     */
    private void jButtonCloseActionPerformed(ActionEvent e){
    	DialogExit();
    }
    /**
     * 关闭窗口
     */
    private void DialogExit(){
    	this.dispose();
    }
    // Variables declaration - do not modify 
    private String DataBase;
    
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonOpen;
    private javax.swing.JComboBox jComboBoxServer;
    private javax.swing.JComboBox jComboBoxWorkspaceName;
    private javax.swing.JLabel jLabelDatabase;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelPassword;
    protected javax.swing.JLabel jLabelServer;
    private javax.swing.JLabel jLabelUser;
    private javax.swing.JPanel jPanelWorkspaceInfo;
    private javax.swing.JTextField jTextFieldDatabase;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JTextField jTextFieldUser;
    private JLabel jLabelEmptyName;
    private JLabel jLabelEmptyServer;
    private JLabel jLabelEmptyUser;
    private JLabel jLabelEmptyLabel;
    // End of variables declaration                   
}

